package com.onpositive.data.diskstorage;

import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Collection;

import com.onpositive.data.AbstractReader;
import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.data.IReadableObjectCollection;
import com.onpositive.data.serializer.readers.DefaultReaderFactory;
import com.onpositive.data.serializer.readers.ObjectCollectionReader;
import com.onpositive.data.serializer.readers.RandomAccessFileAccess;
import com.onpositive.data.units.DateWithAccuracy;

public class CollectionRewriter {

	
	public static void main(String[] args) {
		try {
			IReadableObjectCollection<?>[] cm=new ObjectCollectionReader().getCollection(new RandomAccessFileAccess(new RandomAccessFile("C:\\work\\wiki new\\com.onpositive.fighterspedia\\assets\\serialized.jpg","r")));
			for (IReadableObjectCollection<?> z:cm){
				StoredCollection m=(StoredCollection) z;
				
					Collection<IPropertyMetadata> properties = m.properties();
					for (IPropertyMetadata mq:properties){
						byte[] data = m.getData(mq);
						FileOutputStream fd=new FileOutputStream("C:\\ee\\"+mq.id());
						fd.write(data);
						fd.close();
						RandomAccessFileAccess mm=new RandomAccessFileAccess(new RandomAccessFile("C:\\ee\\"+mq.id(),"r"));
						AbstractReader<?> createReader = DefaultReaderFactory.getInstance().createReader(mm.readInt(), mq);
						createReader.setFile(mm);
						createReader.setMyStart(0);
						if (mq.id().equals(IPropertyConstants.MAIN_PICTURE_PROPERTY_METADATA_ID)){
							System.out.println("A");
						}
						createReader.setMyEnd(data.length);
						for (int a=0;a<m.size();a++){
							Object value = m.getValue(a, mq);
							Object object = createReader.getObject(a);
							System.out.println(object);
							if (value!=null){
								if (object==null){
									throw new IllegalStateException();
								}
								if (value instanceof byte[]){
									if (!Arrays.equals((byte[])value,(byte[])object)){
										throw new IllegalStateException();
									}
									continue;
								}
								if (!value.equals(object)){
									System.out.println(value);
									System.out.println(object);
									if (value instanceof DateWithAccuracy){
										if (value.toString().equals(object.toString())){
											continue;
										}
									}
									throw new IllegalStateException();
								}
							}
						}
					}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
